var Game = require('Game');
cc.Class({
    extends: cc.Component,
 
    properties: {
        // 假设你有一个Label组件用于显示接收到的数据
        
    },
 
    onLoad() {
        this.connectWebSocket();

        var t2 = window.setInterval(function() {
           Game.instance.gameKernel.heart(); 
      
        },3000)
        
       // window.clearInterval(t2)
    },
 
    connectWebSocket() {
      
        // 假设WebSocket服务器的地址是 ws://your-websocket-server/path
     //  let ws = new WebSocket('ws://192.168.31.111:10009');
       
       let ws = new WebSocket('ws://134.175.54.227:10000');
       //let ws = new WebSocket('ws://38.54.14.197:10000');

      // let ws = new WebSocket('ws://43.133.63.190:10001');
       
       // 保存WebSocket实例，以便在其他地方使用
        this.ws = ws; 
        ws.onopen = function(event) {
            console.log("WebSocket 连接已打开");
        };
 
        ws.onerror = function(error) {
            console.log("WebSocket 出错:", error);
           
        };
 
        ws.onmessage = function(event) {
            // 当收到服务器消息时，更新UI
            console.log("服务器发送内容："+event.data); // 输出: 内容
            try {

                var obj=JSON.parse(event.data);
                cc.log('解析main:%s,sub:%s,cmd:%s ' , obj.main,obj.sub,obj.cmd);
                switch (obj.main) {
                    case 200:
                        this.updateUI(obj);
                        break;
                    case 100:
                        this.roomStatus(obj);
                        break;
                    default:
                        break;
                }
            
               
    
              } catch (error) {
                 console.log("解析异常："+error); // 输出: 内容
               }


        }.bind(this);
 
        ws.onclose = function(event) {
            console.log("WebSocket 连接已关闭");
            Game.instance.mPopTips.show('游戏服务器已经关闭，请手动退出');
        };
 
        
    },
 
    updateUI(data) {
        // 
        var cmd= data.cmd;
    
      switch (data.sub) {

        case 0:
            //开始新的游戏
            this.gameStart();
            break;

        case 1:
            //游戏房间信息，每秒更新
          this.gameRoomInfo(cmd);

            break;
        case 2:
            //游戏结果，倒计时结束，开始骰子动画
          this.gameResult(cmd);
            break;
        case 3:
            //用户信息更新
         this.userInfoUpdate(cmd);
            break;
        case 4:
            this.errorMsg(cmd);
            break;
        case 99:
            //创建连接成功
            this.login();
            break;
      
        default:
            break;
      }
    },
    
    login(){
        Game.instance.gameKernel.login();
    },
    liveRoom(){
        this.ws.close(1000, '客户端主动断开');
        cc.log('Websocket=====客户端主动断开');
    },
     
    gameStart(){
        cc.log('Websocket=====gameStart');
        Game.instance.gameKernel.gameStart();
    },
    userInfoUpdate(cmd){
        let cmd0={
            id:10000,
            name:'李白',
            mac:'192.168.31.49',
            score:0,
            select:1,
            betScore:1000,
            betScoreTotal:10000,
        }

        Game.instance.gameKernel.userInfoUpdate(cmd);
    },

    gameRoomInfo(cmd){
     let cmd0={
        gameTimes:0,
        bigPeople:0,
        smallPeople:0,
        bigTotal:0,
        smallTotal:0,
        games:0,
        gameList:[],

     }
     //console.log("gameRoomInfoCmd "+cmd);
     Game.instance.gameKernel.gameRoomInfo(cmd);
    },
    gameResult(cmd){
        let cmd0={
            gameTime:30,
            points:null,
            winScore:null,    
        }
        Game.instance.gameKernel.gameResult(cmd);
       },

       userInfoUpdate(cmd){
        let cmd0={
            id:10000,
            name:'李白',
            mac:'192.168.31.49',
            score:0,
            smallBetScoreTotal:1000,
            bigBetScoreTotal:10000,
        }

        Game.instance.gameKernel.userInfoUpdate(cmd);
    },
    errorMsg(cmd){
        let cmd0={
            code:0,
            msg:'错误信息',  
        }

        Game.instance.mToast.show("获取金币："+cmd.msg,3);
    },

    roomStatus(data){
        var cmd= data.cmd;
        switch (data.sub) {
            case 1:
                Game.instance.mToast.show("最后一局，本局结算后，游戏结束",3);
   
                break;
        
            default:
                break;
        }

    },
   
 
    // 如果需要发送数据到WebSocket服务器
    sendMessage(message) {
        if (this.ws && this.ws.readyState == WebSocket.OPEN) {
            this.ws.send(message);
        }else{
            this.connectWebSocket();
        }
    }
});